﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace MikroObject
{
    public class Data
    {
        public string Server { get; set; }
        public string User { get; set; }
        public string Password { get; set; }
        public string Database { get; set; }

        private string GetString()
        {
            return string.Format("Data Source={0};User Id={1};Password={2};Database={3}", this.Server, this.User, this.Password, this.Database);
        }

        public SqlTransaction OpenSession()
        {
            SqlConnection cn = new SqlConnection(GetString());
            cn.Open();
            return cn.BeginTransaction(IsolationLevel.ReadUncommitted);
        }

        public bool ExecQuery(string cmdText, SqlTransaction trn)
        {
            bool sonuc = false;
            using (SqlCommand cmd = new SqlCommand(cmdText, trn.Connection))
            {
                cmd.Transaction = trn;
                try
                {
                    cmd.ExecuteNonQuery();
                    sonuc = true;
                }
                catch (Exception ex)
                {
                    Logging.GetLog.ErrorLog(ex);
                    sonuc = false;
                }
            }
            return sonuc;
        }

        public DataTable List(string cmdText)
        {
            DataTable sonuc = new DataTable();
            using (SqlConnection cn = new SqlConnection(GetString()))
            {
                using (SqlCommand cmd = new SqlCommand(cmdText, cn))
                {
                    using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
                    {
                        try
                        {
                            adp.Fill(sonuc);
                        }
                        catch (Exception ex)
                        {
                            Logging.GetLog.ErrorLog(ex);
                        }
                    }
                }
                cn.Close();
            }
            return sonuc;
        }

        public object ExecScaler(string cmdText, SqlTransaction trn, params SqlParameter[] prm)
        {
            object sonuc = null;
            using (SqlCommand cmd = new SqlCommand(cmdText, trn.Connection))
            {
                cmd.Transaction = trn;
                try
                {
                    cmd.Parameters.AddRange(prm);
                    sonuc = cmd.ExecuteScalar();
                    cmd.Parameters.Clear();
                }
                catch (Exception ex)
                {
                    Logging.GetLog.ErrorLog(ex);
                }
            }
            return sonuc;
        }

        public bool ExecQuery(string cmdText, SqlTransaction trn, params SqlParameter[] prm)
        {
            bool sonuc = false;
            using (SqlCommand cmd = new SqlCommand(cmdText, trn.Connection))
            {
                cmd.Transaction = trn;
                try
                {
                    cmd.Parameters.AddRange(prm);
                    cmd.ExecuteNonQuery();
                    cmd.Parameters.Clear();
                    sonuc = true;
                }
                catch (Exception ex)
                {
                    Logging.GetLog.ErrorLog(ex);
                    sonuc = false;
                }
            }
            return sonuc;
        }

        public object ExecScaler(string cmdText, SqlTransaction trn)
        {
            object sonuc = null;
            using (SqlCommand cmd = new SqlCommand(cmdText, trn.Connection))
            {
                cmd.Transaction = trn;
                try
                {
                    sonuc = cmd.ExecuteScalar();
                }
                catch (Exception ex)
                {
                    Logging.GetLog.ErrorLog(ex);
                }
            }
            return sonuc;
        }


        private Data() { }
        static Data _instance = null;
        public static Data Get
        {
            get
            {
                if (_instance == null)
                    _instance = new Data();
                return _instance;
            }
        }


    }
}
